var windowName = "fillPhone";
apiready = function () {
    onWindowShow(function () {
        console.log(windowName + " show")

    })
    onWindowHide(function () {
        console.log(windowName + " hide")
    })
    console.log("index page is OK");
    var Page = createVM("#Page", {
        //页面数据
        form: {}
        , phone: ""
        , password: ""
        , usernameRules: [
            {validate: (val) => !!val, message: '必须填写用户名'},
            {validate: (val) => val.length >= 2, message: '用户名长度大于2'}
        ]
        , passwordRules: [
            {validate: (val) => !!val, message: '必须填写密码'},
            {validate: (val) => val.length >= 3 && val.length <= 24, message: '密码长度大于3小于25'}
        ]
        , validateForm: {
            username: '',
            password: ''
        }
        , msg: "获取验证码"
        , cur: 0
    }, {
        //页面方法
        "init": function () {
        },
        sendSms: function () {
            var _this = this;
            var phone = this.phone;

            if (!isTrue(phone)) {
                showToast('请填写手机号');
                return false;
            }
            if (!checkIsPhone(phone)) {
                showToast('请检查手机号');
                return false;
            }

            apiGet("/app/send_sms", {phone: phone}, function (res, code) {
                logJson(res, "/app/send_sms: ")
                if (code == 200) {
                    var int = 0
                    var start = 60
                    int = setInterval(function () {
                        start--
                        Page.msg = start + "s"
                        if (start == 0) {
                            clearInterval(int)
                            Page.msg = "获取验证码"
                        }
                    }, 1000)
                }
                showToast(res.message)
            });
        }
        , login: function () {
            var _this = this;

            var phone = this.phone;
            var code = this.password;
            if (!isTrue(phone)) {
                showToast('请填写手机号')
                return false;
            }
            if (!isTrue(code)) {
                showToast('验证码不能为空')
                return false;
            }
            if (!checkIsPhone(phone)) {
                showToast('请检查手机号')
                return false;
            }

            apiGet("/app/bind/phone", {phone: phone, code: code}, function (res, code) {
                logJson(res, "/app/bind/phone: ")
                showToast(res.message)
                if (code == 200) {
                    setLocData("user", res.data)
                    setLocData("login", 1)
                    setLocData("phone", phone)
                    //设置 auth_token
                    setLocData("auth_token", res.data.phone)
                    goback()
                }
            });

        }
        , kbsubmit: function (event) {
            logJson(event, "event");
            logJson(event.keyCode, "e")
            logJson(this.password, "password")
            if (event.keyCode == 13) {
                this.login();
            }
        }
    })
    Page.init()

}